Lines 0--7 are the various \texttt{include} statements.

Lines 8--13 are a set of \texttt{using} statements to avoid clutter
in the code.

Lines 18--23 create a consumer with a continuous query
of \texttt{SELECT userId, aString, aReal, anInt, RgmaTimestamp FROM
default.userTable}. In this case we have a continuous query which will
start with available published tuples from the last 10 minutes and
receive any new tuples published. It is possible to leave out the
\texttt{historyPeriod} and use \texttt{QueryType}
instead of \texttt{QueryTypeWithInterval}, in which case only newly
published tuples will be received. The query has a timeout of 5
minutes. With a latest or history query we can expect the query to
complete within the timeout. However the continuous query can only be
terminated by an explicit abort call or by the timeout being
exceeded. So we expect this query to take exactly 5 minutes.

Lines 24--46 retrieve all of the results for the query. While there is
data available it will loop without delay. If there is no data
currently available it will sleep for 2 seconds before trying
\texttt{pop} again. The 2000 is the maximum number of tuples to be
returned at once.

Lines 35–43 extract individual fields from the tuple and print them.

Line 47 close the consumer.

Lines 49--53 report any R-GMA exceptions that may occur.

\subsubsection{One-off Queries}
For one-off queries, either history or latest, it is preferable to
check to see if the query aborted. This can be achieved by looking to
see if \texttt{c->hasAborted()} is true after either kind of consumer
loop. This can be the result of hitting the timeout (5minutes in the
above case) or making an explicit \texttt{c->abort()} call. Note that
continuous queries \emph{only} stop by one of these means so there is
no point in checking in that case.

\subsubsection{Resilient Consumer Example}
This example illustrates how to write a resilient consumer according
to the recommendations in section \ref{sec:advice}.  The consumer
retrieves information periodically. If there is a temporary problem it
retries after one minute.

\input{ResilientConsumer-cpp}

Lines 0--9 are the various \texttt{include} statements.

Lines 10--17 are a set of \texttt{using} statements to avoid clutter
in the code.

Line 20 set up initial state for the consumer.

Lines 21--22 set up the consumers select statement.

Line 25 set up the history period for the query.

Lines 26--37 create a new consumer. This uses a continuous information
query starting from 30 seconds in the past so that if the consumer is
restarted data should not be lost. N.B. It is possible that this may
cause some tuples to be retrieved a second time if the system has to
restart the consumer. If there is an RGMATemporaryException then sleep
for 60 seconds before retrying to recreate the consumer.

Lines 38--72 loops forever - each iteration will retrieve data and
check for warnings. While there is data available it will loop without
delay. If there is no data currently available it will sleep for 2
seconds before trying again. See Section \ref{sec:adviceConsumers} for further explanation about the pop and sleep. If
there is an RGMATemporaryException then sleep for 60 seconds before
trying to pop again.

Lines 50--58 extract individual fields from the tuple and print them.

Lines 74--80 report an RGMAPermanentException. As this is considered
to be a permanent problem there is no point retrying the operation.
